//
// Created by dong on 2025/10/21.
//

#include "SPaint.h"
#include "Swindow.h"


void SPaint::setColor(SDL_Color color)
{
    this->color=color;
}

void SPaint::drawRect(const SDL_Rect& dst)
{
    SDL_SetRenderDrawColor(Swindow::render,color.r,color.g,color.b,color.a);
    SDL_RenderFillRect(Swindow::render,&dst);
}

void SPaint::drawRect(int x, int y, int w, int h)
{
    SDL_SetRenderDrawColor(Swindow::render,color.r,color.g,color.b,color.a);
    SDL_Rect rect = {x,y,w,h};
    SDL_RenderFillRect(Swindow::render,&rect);
}

void SPaint::drawText(const string& text, const SDL_Rect& dst)
{
    TTF_Font* font = TTF_OpenFont("../simkai.ttf",20);
    SDL_Surface* surface=TTF_RenderUTF8_Blended(font,text.c_str(),this->color);
    SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,surface);
    SDL_RenderCopy(Swindow::render,temp,NULL,&dst);
    SDL_FreeSurface(surface);
    TTF_CloseFont(font);
}

void SPaint::drawText(int x, int y, const string& text)
{
    TTF_Font* font = TTF_OpenFont("../simkai.ttf",20);
    SDL_Surface* surface=TTF_RenderUTF8_Blended(font,text.c_str(),this->color);
    SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,surface);
    SDL_Rect dst={x,y,surface->w,surface->h};
    SDL_RenderCopy(Swindow::render,temp,NULL,&dst);
    SDL_FreeSurface(surface);
    TTF_CloseFont(font);
}

void SPaint::drawImg(const SDL_Rect& dst, const string& url)
{
    SDL_Surface* img = IMG_Load(url.c_str());
    SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,img);
    SDL_RenderCopy(Swindow::render,temp,NULL,&dst);
    SDL_FreeSurface(img);
    SDL_DestroyTexture(temp);
}

int SPaint::width() const
{
    return w;
}

int SPaint::height() const
{
    return h;
}
